// components/rank-list/rank-list.js
const config = require("../../http/env");
const httpApi = require("../../http/request"); //后续可以改为你自己的域名接口地址

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    currentTab: {
      type: Number,
      value: 0,
    },
    unitid: {
      type: String,
      value: "",
    },
  },
  lifetimes: {
    attached() {
      this.getRankList();
    },
  },
  /**
   * 组件的初始数据
   */
  data: {
    tabList: ["总榜", "日榜"],
    rankList: [],
    myRank: {},
    rewardNum: 0,
    baseUrl: config.baseUrl,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getRankList() {
      httpApi
        .Post("/index/getAnnounce", {
          user_id: wx.getStorageSync("userInfo").id,
          type: this.data.currentTab,
        })
        .then((res) => {
          // console.log('rankList',res.data);
          if (res.status == 0) {
            //处理头像地址---start---
            const rowList=[];
            res.data.forEach((item) => {
              if (!item.headimg) {
                item.headimg = 'https://zsmatch.oss-cn-qingdao.aliyuncs.com/v3/20250327/headimg.png'
              } else {
                item.headimg = !item.headimg.includes("http") ? this.data.baseUrl + item.headimg : item.headimg;
              } 
              rowList.push(item);
            });
            if (!res.myData.headimg) {
              res.myData.headimg = 'https://zsmatch.oss-cn-qingdao.aliyuncs.com/v3/20250327/headimg.png'
            } else {
              res.myData.headimg = !res.myData.headimg.includes("http") ? this.data.baseUrl + res.myData.headimg : res.myData.headimg;
            }
            //处理头像地址---end---

            this.setData({
              rankList: rowList,
              myRank: res.myData,
              rewardNum: res.reward_num,
            });
          } else {
            this.setData({
              rankList: [],
              myRank: {},
              rewardNum: 0,
            });
          }
        });
    },
    closeRank() {
      this.triggerEvent("closeRank", true);
    },
    selectTab(e) {
      console.log("selectTab", e);
      const tabIndex = e.currentTarget.dataset.tab;
      this.setData({
        currentTab: tabIndex,
      });
      this.getRankList();
    },
    // 去PK
    toFight() {
      this.closeRank();
      wx.navigateTo({
        url: "/pages/pk-loading/pk-loading",
      });
    },
    toFightPage() {
      this.closeRank();
      wx.navigateTo({
        url: "/pages/friendVs/friendVs",
      });
    },
    openAuth() {
      this.triggerEvent("openNick", { jumpIf: true });
    },
  },
});
